#include "OtherAGV.h"


void OtherAGV::write(vector<Cart> carts, vector<Cart_item> cart_items, vector<Category> cats, vector<Product> prods, vector<Customer> custs){

	if (!validateCart(carts, cart_items)){
		cout << "Invalid cart. Cart must have at least one cart items" << endl;
	}
	
	if (!validateCartItem(cart_items, prods)){
		cout << "Invalid cart item. Cart item must refer to existing product" << endl;
	}
	
	if (!validateProduct(prods)){
		cout << "Invalid product. Product must have price greater than zero" << endl;
	}
	else{
		ofstream file;
		file.open("catprd.dat");

		//write category and product
		//write category size
		file << cats.size() << endl;

		for (int i=0; i<cats.size(); ++i){
			file << cats.at(i).get_cat_id() << "\t" << cats.at(i).get_cat_name() << endl;
		}

		file << "\t\t\n\t\t\n";

		//write product size
		file << prods.size() << endl;



		for (int i=0; i<prods.size(); ++i){
			file 	<< convertProdId(prods.at(i).get_prod_id()) << "\t"
					<< prods.at(i).get_cat_id() << "\t"
					<< prods.at(i).get_prod_name() << "\t"
					<< prods.at(i).get_prod_price() << endl;
		}

		file.close();

		//write customer
		file.open("cust.dat");

		file << custs.size() << endl;

		for (int i=0; i<custs.size(); ++i){
			file 	<< custs.at(i).get_cust_id() << "\t"
					<< custs.at(i).get_cust_name() << "\t"
					<< custs.at(i).get_cust_addr().get_building_id() << "\t"
					<< custs.at(i).get_cust_addr().get_street_id() << "\t"
					<< custs.at(i).get_cust_addr().get_zip_id() << endl;
		}

		file.close();

		//write cart and cart item
		file.open("carts.dat");

		file << carts.size() << endl;

		for (int i=0; i<carts.size(); ++i){
			file 	<< carts.at(i).get_cart_id() << "\t"
					<< carts.at(i).get_cust_id() << "\t"
					<< carts.at(i).get_purchase_date().year() << "\t"
					<< carts.at(i).get_purchase_date().month() << "\t"
					<< carts.at(i).get_purchase_date().day() << endl;
		}

		file << endl;

		file << cart_items.size() << endl;

		for (int i=0; i<cart_items.size(); ++i){
			file 	<< cart_items.at(i).get_cart_item_id() << "\t"
					<< cart_items.at(i).get_cart_id() << "\t"
					<< convertProdId(cart_items.at(i).get_prod_id()) << "\t"
					<< cart_items.at(i).get_prod_qty() <<  endl;
		}

		file.close();

		cout << "\nData saved succesfully \n\n";
	}
	
}


string OtherAGV::convertProdId(int pid){
	stringstream pid_str;
	if (pid < 100000){
		pid_str << "0";
	}
	if (pid < 10000) {
		pid_str << "0";
	}

	pid_str << pid;

	return pid_str.str();
}

bool OtherAGV::validateCartItem(vector<Cart_item> cart_items, vector<Product> prods){
	/* check if product exists */
	bool product_exists = false;
	for (int j=0; j<cart_items.size(); ++j){
		for (int i=0; i < prods.size(); ++i){
			if (prods.at(i).get_prod_id() == cart_items.at(j).get_prod_id()){ /* if equals */
				product_exists = true;
				break;
			}
		}
	}
	
	return product_exists;
}

bool OtherAGV::validateCart(vector<Cart> carts, vector<Cart_item> cart_items){
	bool exists = false;
	for (int i=0; i<carts.size(); ++i){
		for (int j=0; j<cart_items.size(); ++j){
			if (cart_items.at(j).get_cart_id() == carts.at(i).get_cart_id()){
				exists = true;
				break;
			}
		}
	}

	return exists;
}

bool OtherAGV::validateProduct(vector<Product> prods){
	bool valid = true;
	for (int i=0; i<prods.size(); ++i){
		if (prods.at(i).get_prod_price() <= 0){
			valid = false;
			break;
		}
	}
	
	return valid;
} 